//
//  MyCollectionViewController.m
//  yunyan
//
//  Created by haichuang on 2018/2/26.
//  Copyright © 2018年 haichuang. All rights reserved.
//

#import "MyCollectionViewController.h"
#import "MyCollectionCell.h"
@interface MyCollectionViewController () <UITableViewDelegate, UITableViewDataSource>
{
        BOOL _isEditing;  // 判断UITableView是否处于编辑状态
}
@property (nonatomic, strong) UITableView * collectionTab;
@property (nonatomic, strong)UIButton *deleteBtn;
@end

@implementation MyCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
  
    if (_isShare) {
        
        [self customNaviItemTitle:@"分享记录" isLeft:YES];
        
    }else{
        
        [self customNaviItemTitle:@"我的收藏" isLeft:YES];
    }
    self.collectionTab = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
 
    [self customTabBarButtonTitle:@"编辑" image:nil target:self action:@selector(rightButtonEvent) isLeft:NO];
    [self.view addSubview:self.collectionTab];
    
    WeakSelf;
    [_collectionTab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.equalTo (weakSelf.view);
       
    }];
    
    
    self.collectionTab.delegate = self;
    self.collectionTab.dataSource = self;
    
    [self.collectionTab registerNib:[UINib nibWithNibName:@"MyCollectionCell" bundle:nil] forCellReuseIdentifier:@"MyCollectionCell"];
    
    _deleteBtn = [[UIButton alloc] init];
    [self.view addSubview:_deleteBtn];
    [_deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.right.bottom.equalTo (weakSelf.view);
        make.height.equalTo (@(50.0f));
        
    }];
    [_deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
    _deleteBtn.titleLabel.font = [UIFont systemFontOfSize:18.f];
    _deleteBtn.backgroundColor = RGBA(81, 158, 238, 1);
    _deleteBtn.hidden = YES;
    [_deleteBtn addTarget:self action:@selector(onDeleteBtn) forControlEvents:UIControlEventTouchUpInside];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)rightButtonEvent{
    WeakSelf;
    if (_isEditing) {
        // 当退出编辑时
        self.collectionTab.allowsMultipleSelectionDuringEditing = NO;
        // 取消编辑模式
        [self.collectionTab setEditing:NO animated:YES];
        
        [_collectionTab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo (weakSelf.view.mas_bottom).offset (-60.f);
            
        }];
        
          [self customTabBarButtonTitle:@"编辑" image:nil target:self action:@selector(rightButtonEvent) isLeft:NO];
        _deleteBtn.hidden = YES;
        _isEditing = NO;
    }
    else
    {
      self.collectionTab.allowsMultipleSelectionDuringEditing = YES;
     
        [self.collectionTab setEditing:YES animated:YES];
        [_collectionTab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.top.equalTo (weakSelf.view).offset (-60.f);
            
        }];
          [self customTabBarButtonTitle:@"取消" image:nil target:self action:@selector(rightButtonEvent) isLeft:NO];
        _deleteBtn.hidden = NO;
        _isEditing = YES;
    }
}


-(void)onDeleteBtn{
    [UIUtils showHudFailWithTint:@"删除失败"];
    [self.collectionTab setEditing:NO animated:YES];
}

#pragma mark - UITableViewDelegate, UITableViewDataSource

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10.f;
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return nil;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 107;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MyCollectionCell * cell = [tableView dequeueReusableCellWithIdentifier:@"MyCollectionCell" forIndexPath:indexPath];
    
    return cell;
}

@end
